package com.cncf.service.impl;

import com.cncf.dao.InvalidReportFileMapper;
import com.cncf.entity.InvalidReportFile;
import com.cncf.entity.InvalidReportFileExample;
import com.cncf.entity.Report;
import com.cncf.service.InvalidReportFileService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* @author zj 
*/
@Service
public class InvalidReportFileServiceImpl implements InvalidReportFileService {
    @Autowired
    private InvalidReportFileMapper invalidReportFileMapper;

    @Override
    public int addInvalidReportFile(InvalidReportFile invalidReportFile){
        return invalidReportFileMapper.insert(invalidReportFile);
    }

    @Override
    public int updateInvalidReportFile(InvalidReportFile invalidReportFile){
        return invalidReportFileMapper.updateByPrimaryKeySelective(invalidReportFile);
    }

    @Override
    public InvalidReportFile getInvalidReportFileById(String id){
        return invalidReportFileMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<InvalidReportFile> getAllInvalidReportFile(Integer page, Integer pageSize){
        if (page==null||pageSize==null){
            InvalidReportFileExample invalidReportFileExample=new InvalidReportFileExample();
            return invalidReportFileMapper.selectByExample(invalidReportFileExample);
        }
        int offset = page*pageSize;
        int limit = pageSize;
        //return invalidReportFileMapper.selectAllInvalidReportFileByPage(offset,limit);
        return new ArrayList<InvalidReportFile>();
    }

    @Override
    public int deleteInvalidReportFile(String id){
        return invalidReportFileMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean insertInvalidReportForBatch(String headerString, InputStream is) throws IOException {
        List<InvalidReportFile> invalidReportFileList = new ArrayList<>();
        Workbook workbook = null;
        if (headerString.endsWith(".xls")) {
            System.out.println("excel后缀为+“.xls”");
            workbook = new HSSFWorkbook(is);
        }
        if (headerString.endsWith(".xlsx")) {
            System.out.println("excel后缀为+“.xlsx”");
            workbook = new XSSFWorkbook(is);
        }
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = sheet.iterator();
        //这个if块是为了去掉每个sheet的标题行，之所以要判断是否存在后继元素是因为：
        //读到空的sheet时如果直接执行next()方法会报NoSuchElementException异常.
        //顺便用标题行来判断列数，且必须用标题行来判断
        int colNumber = 0;
        if (iterator.hasNext()) {
            Row row = iterator.next();
            colNumber = row.getPhysicalNumberOfCells();
            if (colNumber != 6) {//有记录但列数不为6
                return false;
            }
        } else {//一行记录都没有，连标题都没有
            return false;
        }

        int rowCount = 0;
        while (iterator.hasNext()) {//这层循环遍历sheet的行
            Row row = iterator.next();
            rowCount++;
            InvalidReportFile invalidReportFile = new InvalidReportFile();

            Cell cell = row.getCell(0);
            if (cell==null){
                throw new RuntimeException("第"+rowCount+"条记录的第一列不能为空");
            }
            cell.setCellType(Cell.CELL_TYPE_STRING);
            invalidReportFile.setId(cell.getStringCellValue());

            cell = row.getCell(1);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            invalidReportFile.setReportNumber(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(2);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_NUMERIC);
            }
            invalidReportFile.setStartDate(cell == null ? null : cell.getDateCellValue());

            cell = row.getCell(3);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            invalidReportFile.setProductId(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(4);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            invalidReportFile.setSendCheckUnit(cell == null ? "" : cell.getStringCellValue());

            cell = row.getCell(5);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
            invalidReportFile.setInvalidReason(cell == null ? "" : cell.getStringCellValue());

            invalidReportFileList.add(invalidReportFile);
        }
        int res = invalidReportFileMapper.insertInvalidReportForBatch(invalidReportFileList);
        return res == rowCount;
    }
}